package com.webframe.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webframe.dao.IJdbcDao;
import com.webframe.service.def.IMenuService;

@SuppressWarnings("unchecked")
@Service
public class MenuService implements IMenuService
{
	@Autowired
	private IJdbcDao		jdbcDao;

	private static String TABLE_MENU = "t_menu";//菜单表

	private static String TABLE_ROLE = "t_role";//角色表

	private static String TABLE_MENU_ROLE = "t_menu_role";//菜单角色关联表

	private static String FIELD_MENU_ID = "ID_";//菜单表字段ID

	private static String FIELD_MENU_PID = "PID_";//菜单表字段pid

	private static String FIELD_ROLE_ID = "ID_";//角色表字段id

	private static String FIELD_ROLE_ROLE = "ROLE_";//角色表字段role

	private static String FIELD_MENU_ROLE_MENUID = "MENU_ID_";//关联表字段菜单id

	private static String FIELD_MENU_ROLE_ROLEID = "ROLE_ID_";//关联表字段角色id


	// 插入或修改菜单
	@SuppressWarnings("rawtypes")
	public Boolean doInsertOrUpdate(Map<String, Object> object) throws Exception
	{
		Map map = doSort(object);
		jdbcDao.doInsertOrUpdate(map);
		return true;
	}

	// 删除 菜单
	@SuppressWarnings("rawtypes")
	public Boolean doDeleteMenuByID(String menuID) throws Exception
	{
		// 判断此ID下有没有子菜单,如果有则先删除所有子菜单
		List childMenus = GetMenusByPID(menuID);
		if (null != childMenus && childMenus.size() > 0)
		{
			for (int i = 0; i < childMenus.size(); i++)
			{
				String ID = ((Map) childMenus.get(i)).get(FIELD_MENU_ID).toString();
				doDeleteMenuByID(ID);
			}
		}
		// 删除菜单
		jdbcDao.doDeleteExecuteByID(TABLE_MENU, FIELD_MENU_ID, menuID);
		return true;
	}

	// 菜单排序
	public Map<String, Object> doSort(Map<String, Object> obj) throws Exception
	{
		Integer position = Integer.parseInt(obj.get("POSITION_").toString());
		// 排第一位
		if (position == 0)
		{
			String pid = (String) obj.get("PID_");
			if (null != pid && !"".equals(pid))
			{
				List list = GetMenusByPID(pid);
				if (null != list && list.size() > 0)
				{
					for (int i = 0; i < list.size(); i++)
					{
						Map map = (Map) list.get(i);
						map.put("POSITION_",
							Integer.parseInt(map.get("POSITION_").toString()) + 1);
						jdbcDao.doInsertOrUpdate(TABLE_MENU, map, "ID_");
					}
				}
			}
			obj.put("POSITION_", 1);
			return obj;
		}
		// 排最后一位
		else if (position == -1)
		{
			String pid = (String) obj.get("PID_");
			if (null != pid && !"".equals(pid))
			{
				List list = GetMenusByPID(pid);
				Integer maxPosition = 0;
				if (null != list && list.size() > 0)
				{
					Map map = (Map) list.get(list.size() - 1);
					maxPosition = Integer.parseInt(map.get("POSITION_").toString());
				}
				obj.put("POSITION_", maxPosition + 1);
				return obj;
			}
		}
		// 排中间
		else
		{
			String pid = (String) obj.get("PID_");
			if (null != pid && !"".equals(pid))
			{
				List list = GetMenusByPID(pid);
				if (null != list && list.size() > 0)
				{
					for (int i = 0; i < list.size(); i++)
					{
						Map map = (Map) list.get(i);
						if (Integer.parseInt(map.get("POSITION_").toString()) == position)
						{
							for (int j = i + 1; j < list.size(); j++)
							{
								Map tMap = (Map) list.get(j);
								tMap.put(
									"POSITION_",
									Integer.parseInt(tMap.get("POSITION_").toString()) + 1);
								jdbcDao.doInsertOrUpdate(TABLE_MENU, tMap, "ID_");
							}
							break;
						}
					}
				}
			}
			obj.put("POSITION_",
				Integer.parseInt(obj.get("POSITION_").toString()) + 1);
			return obj;
		}
		return null;
	}

	// 通过sql语句删除权限
	public Boolean doDeleteRole(String menuID, String roleID) throws Exception
	{
		String sql = "delete from "
					+ TABLE_MENU_ROLE
					+ " where "
					+ FIELD_MENU_ROLE_ROLEID
					+ " = '"
					+ roleID
					+ "' and "
					+ FIELD_MENU_ROLE_MENUID
					+ " = '"
					+ menuID
					+ "' ";
		jdbcDao.doDeleteBySql(sql);
		return true;
	}

	// 增加新的权限
	public Boolean doInsertNewRole(Map<String, Object> object) throws Exception
	{
		jdbcDao.doInsertOrUpdate(TABLE_MENU_ROLE, object, "ID_");
		return true;
	}

	// 根据PID查询所有子菜单
	public List GetMenusByPID(String PID) throws Exception
	{
		String sql = "SELECT * FROM "
					+ TABLE_MENU
					+ " WHERE "
					+ FIELD_MENU_PID
					+ " ='"
					+ PID
					+ "' ORDER BY POSITION_";
		return jdbcDao.doQueryBySql(sql);
	}

	// 根据menuId查询具有的权限
	public List getRolesByMenuID(String menuID) throws Exception
	{
		String sql = "SELECT "
					+ TABLE_ROLE
					+ '.'
					+ FIELD_ROLE_ROLE
					+ " from "
					+ TABLE_ROLE
					+ " inner join "
					+ TABLE_MENU_ROLE
					+ " on "
					+ TABLE_ROLE
					+ '.'
					+ FIELD_ROLE_ID
					+ " = "
					+ TABLE_MENU_ROLE
					+ '.'
					+ FIELD_MENU_ROLE_ROLEID
					+ " where "
					+ TABLE_MENU_ROLE
					+ '.'
					+ FIELD_MENU_ROLE_MENUID
					+ " ='"
					+ menuID
					+ "'";
		return jdbcDao.doQueryBySql(sql);
	}

	// 判断是否具有子菜单
	public String hasSubMenus(String menuID) throws Exception
	{
		if (null == menuID || "".equals(menuID)) { return "true"; }
		List list = GetMenusByPID(menuID);
		if (null != list && list.size() > 0)
		{
			return "false";
		} else return "false";
	}
}
